//
//  MyQRCodeViewController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 22/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit
import SnapKit

class MyQRCodeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "我的名片"
        view.addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.width.height.equalTo(self.view.snp.width).multipliedBy(0.5)
        }
        iconView.image = creatQRCode()
        
    }
    //MARK:生产二维码
    func creatQRCode() -> UIImage {
        // 创建滤镜
        let fillter = CIFilter(name: "CIQRCodeGenerator")
        // 还原滤镜的默认属性
        fillter?.setDefaults()
        
        // 设置要生产的二维码数据
        fillter?.setValue("GML".data(using: String.Encoding.utf8), forKey: "inputMessage")
        // 从滤镜中取出生成好的图片
        let ciImage = fillter?.outputImage
        
        // 创建头像
        
        // 合成图片
        
        return createNonInterpolatedUIImageFormCIImage(image: ciImage!, size: 300)
    }
    /**
     合成图片
     
     :param: bgImage   背景图片
     :param: iconImage 头像
     */
    private func creteImage(bgImage: UIImage, iconImage: UIImage) -> UIImage
    {
        // 1.开启图片上下文
        UIGraphicsBeginImageContext(bgImage.size)
        // 2.绘制背景图片
        bgImage.draw(in: CGRect(origin: CGPoint.zero, size: bgImage.size))
        // 3.绘制头像
        let width:CGFloat = 50
        let height:CGFloat = width
        let x = (bgImage.size.width - width) * 0.5
        let y = (bgImage.size.height - height) * 0.5
        iconImage.draw(in: CGRect(x: x, y: y, width: width, height: height))
        
        // 4.取出绘制号的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        // 5.关闭上下文
        UIGraphicsEndImageContext()
        // 6.返回合成号的图片
        return newImage!
    }
    
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = image.extent.integral
        
        let scale: CGFloat = min(size/extent.width, size/extent.height)
        
        // 1.创建bitmap;
        let width = extent.width * scale
        let height = extent.height * scale
        let cs: CGColorSpace = CGColorSpaceCreateDeviceGray()
//        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        let bitmapRef = CGContext.init(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 9, bytesPerRow: 0, space: cs, bitmapInfo: 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImage = context.createCGImage(image, from: extent)!
        
//        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.none)
        bitmapRef.scaleBy(x: scale, y: scale)
        bitmapRef.draw(bitmapImage, in: extent)
//        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImage = bitmapRef.makeImage()!
        
        return UIImage(cgImage: scaledImage)
    }

    
    private lazy var iconView:UIImageView = UIImageView()

}
